package com.glorysoft.sunnypda.ui.reportfault

import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.base.Constant
import com.glorysoft.sunnypda.entity.RepairInfoEntity
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.model.RepairRepository
import com.glorysoft.sunnypda.ui.reportrepair.ReportRepairViewModel
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: ReportRepairViewModel
 * Author: XJH
 * Date: 2022/1/7 16:13
 * Description:
 * History:
 */
class ReportFaultListViewModel : BaseViewModel() {

    val repairRepository: RepairRepository by lazy { RepairRepository() }
    var repairAllList: ArrayList<RepairInfoEntity> = arrayListOf()

    val onClick: ReportRepairViewModel.ReportClick = object : ReportRepairViewModel.ReportClick {
        override fun onReportItemClick(item: RepairInfoEntity) {


        }

    }

    val onReminderClick: ReminderClick = object : ReminderClick {
        override fun onReminderItemClick(item: RepairInfoEntity) {
            val map = JsonObject()
            map.addProperty("RFR_ID", item.RFR_ID)
            launchOnlyResult({
                repairRepository.getReminder(map)
                defUI.toastEvent.value = ToastMessageBean("催单成功", true)
            })
        }

    }

    var repairItems = ObservableArrayList<RepairInfoEntity>()
    val unFinishList = ArrayList<RepairInfoEntity>()
    val finishList = ArrayList<RepairInfoEntity>()
    val runningList = ArrayList<RepairInfoEntity>()
    var repairBinding =
        ItemBinding.of<RepairInfoEntity>(BR.repairItem, R.layout.item_fault_repair_list)
            .bindExtra(BR.onReportClick, onClick)
            .bindExtra(BR.onReminderClick, onReminderClick)

    fun initRepairForUser(status: Int, eqp: String) {
        val map = JsonObject()
        map.addProperty("workNo", Constant.CURRENT_USER)
        map.addProperty("name", Constant.CURRENT_USER_NAME)
        launchOnlyResult({
            val repairListFroUser = repairRepository.getRepairListFroUser(map)
            repairListFroUser.RFR_LIST.forEach {
//                when (it.RFR_TYPE) {
//                    "0" -> it.RepairType = "一般故障"
//                    "1" -> it.RepairType = "设备问题"
//                    "2" -> it.RepairType = "程序问题"
//                    else -> {
//                        it.RepairType = it.RFR_TYPE
//                    }
//                }
                it.RepairType = if (it.RFR_TYPE == null)"" else it.RFR_TYPE
            }
            repairAllList.clear()
            repairAllList.addAll(repairListFroUser.RFR_LIST)
            updateList(status, eqp)
        })
    }

    fun initRepairForEqp(status: Int, eqp: String) {
        updateList(status, eqp)
    }


    fun updateList(status: Int, eqp: String) {
        repairItems.clear()
        unFinishList.clear()
        finishList.clear()
        runningList.clear()
        if (repairAllList.isNotEmpty()) {
            repairAllList.forEach {
                if (eqp.isEmpty()) {
                    if (it.RFR_CURR_STATE == status) {
                        repairItems.add(it)
                    }
                    when (it.RFR_CURR_STATE) {
                        0 -> {
                            unFinishList.add(it)
                        }
                        1 -> {
                            runningList.add(it)
                        }
                        2 ->{
                            finishList.add(it)
                        }
                    }
                } else {
                    if (it.RFR_CURR_STATE == status && it.EQP_ID == eqp) {
                        repairItems.add(it)
                    }
                    when (it.RFR_CURR_STATE) {
                        0 -> {
                            if (it.EQP_ID == eqp){
                                unFinishList.add(it)
                            }
                        }
                        1 -> {
                            if (it.EQP_ID == eqp){
                                runningList.add(it)
                            }
                        }
                        2 ->{
                            if (it.EQP_ID == eqp){
                                finishList.add(it)
                            }
                        }
                    }
                }

            }
            defUI.callObserve.value = CallObserveBean(0)
        } else {
            defUI.toastEvent.value = ToastMessageBean("服务器未返回数据", false)
        }
    }

    interface ReminderClick {
        fun onReminderItemClick(item: RepairInfoEntity)
    }
}